package com.chen.utils;


import java.util.List;

import com.chen.dto.StudentExamChartDTO;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.PointerType;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.code.X;
import com.github.abel533.echarts.code.Y;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Line;

/**
 * 学生考试信息 图表 Json 数据 生成
 */
public class StudentExamInfoCharts {


    public static String createExamCountBarJson(List<StudentExamChartDTO> examInfos) {
        GsonOption option = new GsonOption();
        option.title().text("学生考试次数统计").x(X.center).y(Y.top).borderWidth(1).textStyle().color("#438EB9");
        option.toolbox().show(true).feature(Tool.mark, Tool.restore, new MagicType(Magic.bar, Magic.line), Tool.saveAsImage).x(X.right).y(Y.top);
        option.tooltip().show(true).formatter("{b} {c}次");

        option.legend().data("学生考试次数统计").x(X.center).y(Y.bottom);

        Bar bar = new Bar();

        //值轴
        ValueAxis valueAxis = new ValueAxis();
        //设置y轴不出现小数值
        valueAxis.interval(1);
        valueAxis.axisLabel().formatter("{value} 次");
        option.yAxis(valueAxis);

        //类目轴
        CategoryAxis categoryAxis = new CategoryAxis();
        categoryAxis.axisLabel().interval(0);

        for (StudentExamChartDTO studentExamInfo : examInfos) {
            bar.data(studentExamInfo.getExamSum());
            categoryAxis.data(studentExamInfo.getStudentName());
        }

        bar.barCategoryGap("20");
        option.xAxis(categoryAxis);
        option.series(bar);

        return option.toString();
    }


    public static String createAvgCountLineJson(List<StudentExamChartDTO> examInfos) {
        GsonOption option = new GsonOption();
        option.title().text("学生考试平均成绩统计").x(X.center).y(Y.top).borderWidth(1).textStyle().color("#438EB9");
        option.toolbox().show(true).feature(Tool.mark, Tool.restore, new MagicType(Magic.bar, Magic.line), Tool.saveAsImage).x(X.right).y(Y.top);
        option.tooltip().show(true).trigger(Trigger.axis).axisPointer().type(PointerType.cross).crossStyle().color("#999");

        option.legend().data("考试次数", "平均分").x(X.center).y(Y.bottom);

        Line line = new Line("考试次数").smooth(true);
        Bar bar = new Bar("平均分");

        //值轴
        ValueAxis valueAxis = new ValueAxis();
        //设置y轴不出现小数值
        valueAxis.interval(1);
        valueAxis.axisLabel().formatter("{value} 次");
        //option.yAxis(valueAxis);

        ValueAxis valueAxis1 = new ValueAxis();
        valueAxis1.interval(1);
        valueAxis1.axisLabel().formatter("{value} 分");
        option.yAxis(valueAxis, valueAxis1);

        //类目轴
        CategoryAxis categoryAxis = new CategoryAxis();
        categoryAxis.axisLabel().interval(0);

        for (StudentExamChartDTO studentExamInfo : examInfos) {
            if (studentExamInfo.getAvgScore() != null || studentExamInfo.getExamSum() != 0) {
                bar.data(studentExamInfo.getAvgScore()/studentExamInfo.getExamSum());
            } else {
                bar.data("暂无记录");
            }
            line.data(studentExamInfo.getExamSum());
            categoryAxis.data(studentExamInfo.getStudentName());
        }

        //实现双 y 轴，使用 yAxisIndex() 指定应用到哪个 y 轴
        bar.barCategoryGap("20").yAxisIndex(1);
        option.xAxis(categoryAxis);
        option.series(bar, line);

        return option.toString();
    }


    public static String createStudentExamLineJson(List<StudentExamChartDTO> examInfos) {
        GsonOption option = new GsonOption();
        option.title().text("学生考试试卷得分统计").x(X.center).y(Y.top).borderWidth(1).textStyle().color("#438EB9");
        option.toolbox().show(true).feature(Tool.mark, Tool.restore, new MagicType(Magic.bar, Magic.line), Tool.saveAsImage).x(X.right).y(Y.top);
        option.tooltip().show(true).formatter("{b} {c}分").trigger(Trigger.axis);
        option.legend().data("得分").x(X.center).y(Y.bottom);

        Line line = new Line("试卷得分");

        //值轴
        ValueAxis valueAxis = new ValueAxis();
        //设置y轴不出现小数值
        valueAxis.interval(1);
        valueAxis.axisLabel().formatter("{value} 分");
        option.yAxis(valueAxis);


        //类目轴
        CategoryAxis categoryAxis = new CategoryAxis();
        categoryAxis.axisLabel().interval(0).rotate(-30);

        for (StudentExamChartDTO studentExamInfo : examInfos) {
            line.data(studentExamInfo.getExamScore());
            categoryAxis.data(studentExamInfo.getExamPaperName());
        }

        option.xAxis(categoryAxis);
        option.series(line);

        System.out.println(option.toString());
        return option.toString();
    }
}
